import type { RouteRecordRaw } from 'vue-router'
import type { Component } from 'vue'
import type { MenuItem } from '@/types/user'
// 路由组件映射表
export const routeComponentMap: Record<string, () => Promise<Component>> = {
  '/home': () => import('@/view/home/HomePage.vue'),
  // '/user': 移除，因为用户管理只是菜单分组，不需要页面组件
  '/user/info': () => import('@/view/user/UserInfo.vue'),
  '/user/role': () => import('@/view/user/RoleConfig.vue'),
  '/content': () => import('@/view/content/ContentIndex.vue'),
  // '/content/shop': 移除，因为店铺管理只是菜单分组，不需要页面组件
  '/content/shop/audit': () => import('@/view/content/shop/ShopAudit.vue'),
  // '/content/publisher': 移除，因为发行商管理只是菜单分组，不需要页面组件
  '/content/script/publisher': () => import('@/view/content/publisher/ScriptManage.vue'),
  '/content/publisher/scriptaudit': () => import('@/view/content/publisher/ScriptAudit.vue'),
  '/content/publisher/audit': () => import('@/view/content/publisher/PublisherAudit.vue'),
  '/content/script': () => import('@/view/content/ScriptManage.vue'),
  '/content/script/shop': () => import('@/view/content/shop/ScriptManage.vue'),
  '/mall': () => import('@/view/mall/mall.vue'),
}

// 常量路由
export const constantRoutes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/view/login/LoginPage.vue'),
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('@/view/register/RegisterPage.vue'),
  },
  {
    path: '/personal',
    name: 'Personal',
    component: () => import('@/view/personal/personal.vue'),
  },
  {
    path: '/publisher/register',
    name: 'PublisherRegister',
    component: () => import('@/view/content/publisher/register.vue'),
    meta: {
      title: '注册发行商',
      icon: 'User'
    }
  },
  {
    path: '/shop/register',
    name: 'ShopRegister',
    component: () => import('@/view/content/shop/register.vue'),
    meta: {
      title: '注册店铺',
      icon: 'User'
    }
  },
  {
    path: '/404',
    name: '404',
    component: () => import('@/view/error/404.vue'),
  },
]

// 异步路由
export const asyncRoutes = []

// 静态路由配置（每个角色都能访问的路由）
export const staticLayoutRoutes: RouteRecordRaw[] = [
  {
    path: '/personal',
    name: 'Personal',
    component: () => import('@/view/personal/personal.vue'),
    meta: {
      title: '个人信息',
      icon: 'User'
    }
  },
  {
    path: '/home',
    name: 'Home',
    component: () => import('@/view/home/HomePage.vue'),
    meta: {
      title: '首页',
      icon: 'HomeFilled'
    }
  },

]

// 任意路由
export const anyRoute = {
  path: '/:pathMatch(.*)*',
  name: 'Any',
  redirect: '/404',
}

// 将菜单数据转换为路由配置
export function generateRoutes(menuList: MenuItem[]): RouteRecordRaw[] {
  const routes: RouteRecordRaw[] = []


  // 处理菜单分组：将有组件的菜单项直接作为布局的子路由
  function processMenuForRoutes(menuList: MenuItem[]): RouteRecordRaw[] {
    const routes: RouteRecordRaw[] = []

    function extractRoutes(menu: MenuItem) {
      // 如果当前菜单有对应的组件，创建路由
      if (routeComponentMap[menu.menuUrl]) {
        const route = {
          path: menu.menuUrl,
          name: menu.menuName,
          component: routeComponentMap[menu.menuUrl]!,
          meta: {
            title: menu.menuName,
            id: menu.id,
          },
        } as RouteRecordRaw
        routes.push(route)
      }

      // 如果有子菜单，递归处理子菜单
      if (menu.children && menu.children.length > 0) {
        menu.children.forEach(child => extractRoutes(child))
      }
    }

    menuList.forEach(menu => extractRoutes(menu))
    return routes
  }

  // 处理菜单，提取所有有效的路由
  const validRoutes = processMenuForRoutes(menuList)

  // 合并动态路由和静态路由
  const allRoutes = [...validRoutes, ...staticLayoutRoutes]

  // 添加布局路由作为根路由
  const layoutRoute: RouteRecordRaw = {
    path: '/',
    name: 'Layout',
    component: () => import('@/view/layout/layoutPage.vue'),
    redirect: '/home',
    children: allRoutes,
  }
  routes.push(layoutRoute)

  return routes
}
